package {{.TableDetailResp.PackageName}}
{{$shortStructName := .TableDetailResp.StructName}}
{{if not .IgnoreTablePrefix}}{{$shortStructName = ReplaceStrInsensitive .TableDetailResp.StructName .TableDetailResp.TablePrefix ""}}{{end}}
{{- $lowerShortStructName := FirstLetterLower $shortStructName}}
import (
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/models/{{.TableDetailResp.PackageName}}"
	"MoSkeleton/services/base"
	s1 "MoSkeleton/web/pogos/request/{{.TableDetailResp.PackageName}}"
	s2 "MoSkeleton/web/pogos/response/{{.TableDetailResp.PackageName}}"
	"errors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strings"
)

type {{.TableDetailResp.StructName}}Service struct {
	base.BaseService
}

func (d *{{.TableDetailResp.StructName}}Service) Get{{$shortStructName}}Page(pageReq *s1.{{.TableDetailResp.StructName}}PageReq) (*response.PageResult, error) {
    db := d.get{{$shortStructName}}Vo(nil, pageReq)
	result := make([]*s2.{{.TableDetailResp.StructName}}ItemResp, 0)

	var total int64
	if err := db.Count(&total).Offset(pageReq.Offset()).Limit(pageReq.PageSize).Find(&result).Error; err != nil {
		return nil, err
	}
	pageResult := response.NewFromPageInfoAndData(&pageReq.PageInfo, &result, total)
	return pageResult, nil
}

func (d *{{.TableDetailResp.StructName}}Service) Add{{$shortStructName}}(addReq *s1.{{.TableDetailResp.StructName}}AddReq) (bool, error) {
    {{FirstLetterLower .TableDetailResp.StructName}} := &{{.TableDetailResp.ModuleName}}.{{.TableDetailResp.StructName}}{}
    if err := utils.CopyStruct({{FirstLetterLower .TableDetailResp.StructName}}, addReq); err != nil {
        return false, err
    }
    result := base.BaseDb.Create({{FirstLetterLower .TableDetailResp.StructName}})
    return result.RowsAffected == 1, result.Error
}

func (d *{{.TableDetailResp.StructName}}Service) Delete{{$shortStructName}}ByIds(c *gin.Context, ids *[]uint64) (bool, error) {
	{{FirstLetterLower .TableDetailResp.StructName}} := &{{.TableDetailResp.ModuleName}}.{{.TableDetailResp.StructName}}{}
    err := base.BaseDb.Transaction(func(tx *gorm.DB) error {
        if _, err := {{FirstLetterLower .TableDetailResp.StructName}}.DeleteByIds(tx, ids, {{FirstLetterLower .TableDetailResp.StructName}}); err != nil {
            return err
        }
        return nil
    })
    return err == nil, err
}

func (d *{{.TableDetailResp.StructName}}Service) Get{{$shortStructName}}ById(c *gin.Context, id uint64) (*s2.{{.TableDetailResp.StructName}}ItemResp, error) {
	{{FirstLetterLower .TableDetailResp.StructName}} := &s2.{{.TableDetailResp.StructName}}ItemResp{}
	if err := base.BaseDb.Model(&{{.TableDetailResp.ModuleName}}.{{.TableDetailResp.StructName}}{}).Where("id = ?", id).First({{FirstLetterLower .TableDetailResp.StructName}}).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, nil
		}
		return nil, err
	}
	return {{FirstLetterLower .TableDetailResp.StructName}}, nil
}

func (d *{{.TableDetailResp.StructName}}Service) Update{{$shortStructName}}ById(c *gin.Context, updateReq *s1.{{.TableDetailResp.StructName}}UpdateReq) (ok bool, err error) {
	{{FirstLetterLower .TableDetailResp.StructName}} := &{{.TableDetailResp.ModuleName}}.{{.TableDetailResp.StructName}}{}

	if err = utils.CopyStruct({{FirstLetterLower .TableDetailResp.StructName}}, updateReq); err != nil {
		ok = false
		return
	}
	err = base.BaseDb.Transaction(func(tx *gorm.DB) error {

		if err := d.GetDb(tx).Omit("del_flag").Updates({{FirstLetterLower .TableDetailResp.StructName}}).Error; err != nil {
			return err
		}
		ok = true
		return nil
	})
	return
}


{{if ContainField  .TableDetailResp.Columns "status"}}
func (d *{{.TableDetailResp.StructName}}Service) Update{{$shortStructName}}Status(c *gin.Context, req *s1.Change{{.TableDetailResp.StructName}}StatusReq) (bool, error) {
	{{FirstLetterLower .TableDetailResp.StructName}} := &{{.TableDetailResp.ModuleName}}.SysRole{
		MoModel: models.MoModel{ID: req.ID},
	}

	return {{FirstLetterLower .TableDetailResp.StructName}}.UpdateStatus(nil, req.Status)
}
{{end}}

func (d *{{.TableDetailResp.StructName}}Service) get{{$shortStructName}}Vo(tx *gorm.DB, pageReq *s1.{{.TableDetailResp.StructName}}PageReq) *gorm.DB {
	db := d.GetDb(tx).Table("{{.TableDetailResp.SourceTableName}} r")

	beginTime, ok := pageReq.GetParamValue("beginTime")
	if ok && beginTime != "" {
		if beginTime = pageReq.FormatBeginTime(beginTime); beginTime != "" {
			db.Where("r.created_at >= ?", beginTime)
		}
	}
	endTime, ok := pageReq.GetParamValue("endTime")
	if ok && endTime != "" {
		if endTime = pageReq.FormatEndTime(endTime); endTime != "" {
			db.Where("r.created_at <= ?", endTime)
		}
	}
	{{if ContainField .TableDetailResp.Columns "list_order"}}
	db.Order("r.list_order")
	{{end}}
	return db
}